﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;

namespace VisionRoc_Measure
{
    public partial class FormUser : Form
    {
        public FormUser()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景 
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲  
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.DoubleBuffered = true;//避免时间显示控件闪烁
            InitializeComponent();
        }


        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//标签是否为左键
        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }
        private void Frm_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void Frm_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }




        private void btn_Close_Click(object sender, EventArgs e)
        {

            if (DialogResult.OK == MessageBox.Show("是否退出登录", "退出询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                this.Close();
            }
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Admin" && textBox2.Text == DateTime.Now.ToString("HHmm"))
            {
                if (Global.UserState == 1)
                {
                    MessageBox.Show("已经是管理员权限!");
                    btn_Login.Enabled = false;
                    return;
                }
                MessageBox.Show("登录管理员权限成功!");
                // 发布日志事件
                Global.AddLogInfoEvent(LogLevel.OK, "管理员权限登录成功！");
                Global.UserState = 1;
                btn_Login.Enabled = false;
                this.Close();
            }
            else
            {
                MessageBox.Show("密码错误！");
                Global.UserState = -1;
            }
        }

        private void FormUser_Load(object sender, EventArgs e)
        {
          
            if (Global.UserState == 1)
            {
                btn_Login.Enabled = false;
            }
            this.textBox2.KeyUp += TextBox2_KeyUp; //文本回车事件
                                                   //加载程序语言
            string result = "";
            Global.m_MultiLanguage.LControls = new List<Control>();
            Global.m_MultiLanguage.GetControls(this);
            if (!Global.m_MultiLanguage.LoadLanguage(this, out result))
            {
                MessageBox.Show(result + "\r\n程序当前语言可能异常，如果影响正常使用请重新启动程序", "加载语言", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void TextBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Control || e.KeyCode == Keys.Enter)
            {
                if (textBox1.Text == "Admin"  && textBox2.Text == DateTime.Now.ToString("HHmm"))
                {
                    //this.errorProvider1.Clear();
                    if (Global.UserState == 1)
                    {
                        MessageBox.Show("已经是管理员权限!");
                        btn_Login.Enabled = false;
                        return;
                    }
                    MessageBox.Show("登录管理员权限成功!");
                    // 发布日志事件
                    Global.AddLogInfoEvent(LogLevel.OK, "管理员权限登录成功！");
                    Global.UserState = 1;
                    btn_Login.Enabled = false;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("密码错误！");
                    Global.UserState = -1;
                }
            }
        }
    }
}
